package com.leetcode.p0;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test46_2 {

    private final List<List<Integer>> res  = new ArrayList<>();
    private       List<Integer>       list = new ArrayList<>();

    private int length = 0;

    public List<List<Integer>> permute(int[] nums) {
        length = nums.length;
        if (length <= 0) {
            return res;
        }
        for (int i : nums) {
            list.add(i);
        }

        helper(0);

        return res;
    }

    private void helper(int first) {
        if (first == length) {
            res.add(new ArrayList<>(list));
            return;
        }

        for (int i = first; i < length; i++) {
            Collections.swap(list, first, i);
            helper(first + 1);
            Collections.swap(list, first, i);
        }
    }

    public static void main(String[] args) {
        Test46_2 test = new Test46_2();
        System.out.println(test.permute(new int[]{1, 2, 3}));
    }

}
